package cn.itsource.pethome.org.controller;

import cn.itsource.pethome.org.domain.Employee;
import cn.itsource.pethome.org.query.EmployeeQuery;
import cn.itsource.pethome.org.service.IEmployeeService;
import cn.itsource.pethome.utils.AjaxResult;
import cn.itsource.pethome.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody Employee employee){
        if(employee.getId() == null){
            employeeService.save(employee);
        }else{
            employeeService.update(employee);
        }
        return AjaxResult.me();
    }

    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        employeeService.delete(id);
        return AjaxResult.me();
    }

    @GetMapping(value = "/{id}")
    public Employee selectById(@PathVariable("id") Long id){
        return employeeService.loadById(id);
    }

    @GetMapping
    public List<Employee> selectAll(){
        return employeeService.loadAll();
    }

    @PostMapping
    public PageResult<Employee> selectByPage(@RequestBody EmployeeQuery query){
        return employeeService.selectByPage(query);
    }

    @PatchMapping
    public AjaxResult deletes(@RequestBody Long[] ids){
        employeeService.deletes(ids);
        return AjaxResult.me();
    }
}
